<?php
class Third_Init{
	
	public $view, $entity, $config, $session, $user, $cache, $controller;
	public $operationId, $operationTags;
	
	function __construct( $entity = NULL ){

		//CARREGA CONFIGURACOES GERAIS
		$this->config = Zend_Registry::get('configs');
		
		// SESSION
		$this->session = new Zend_Session_Namespace( $this->config->session_name );
		$this->session->setExpirationSeconds(6000000);
		
		// VALIDA ESTRUTURA DE PASTAS
		$this->validatePaths();
		
		// CARREGA O OBJETO DO SMARTY
		$this->view = Zend_Registry::get("view");
		
		// CACHE
		$this->cache = Zend_Registry::get('cache');
		
/*		//CONTROLLER
		$this->controller = Zend_Registry::get('controller');
	*/	
		//ENTIDADE
		if( $entity )
			$this->setEntity( $entity );

	}
	
	function validatePaths(){
		
		$verified = $this->session->pathVerified;
		
		if( !$verified ){

			$error = false;

			if( !is_dir( $this->config['site']['cache_path'] )  && !($error) ){ 
				$error = "CACHE PATH NOT EXISTS";
			}
			
			if( $error ){
				throw new Exception( $error );
			} else{
				$this->session->pathVerified = true;
			}
		}	

	}
	
	function getOperationCache(){
		$cached = $this->cache->load( $this->operationId );
		return $cached;
	}
	
	function setEntity( $entity ){
		$this->entity = new Third_Entity( $entity );
	}

	function setOperationId( $operationId, $operationTags = array() ){
		$this->operationId = $operationId;
		
		if( is_array( $operationTags ))
			$this->setOperationTags( $operationTags );
	}

	function getOperationId(){
		return $this->operationId;
	}

	function setOperationTags( $operationTags = array() ){
		$this->operationTags = $operationTags;
	}
	
	function getOperationTags(){
		return $this->operationTags;
	}
	
	function unsetOperationId(){
		unset( $this->operationId );
		unset( $this->operationTags );
	}
	
	function registerSession( $userData, $action = NULL){

		// LIMPA DADOS GRAVADOS		
		unset( $this->session->userData );

		$this->session->userData = $userData;

		if($action == 'login'){
			$this->session->logged = true;
			$this->session->setExpirationSeconds(1320);
		}
	}
/*
 * VERICADACAO DE USUARIOS LOGADOS
 * */
	function loggedUser(){

		$user =  $this->session->userData;

		$this->logged = $this->isLogged();
		
		if( $this->logged ){

			$this->view->assign("isLogged", $this->logged );
			$this->view->assign("loggedUser", $user );

			$this->user = $user;
			
			return $user;
		} else {
			return NULL;
		}	
	}

	function ownerUser( $id ){
		if( $this->isLogged() && !empty( $id )){

			if( $this->user['id'] == $id )
					return true;
		}
		return false;	
	}
	
	function isLogged(){
		return $this->session->logged;
	}

	function isLoggedVerify(){
		if( !$this->isLogged() )
			header("Location:". $this->controller->getBaseUrl());
	}
	/**
	 * METODS EXTRAS
	 */
	    function randomValue($length=8, $strength=4){

    	$vowels = 'aeuy';
		$consonants = 'bdghjmnpqrstvz';
		
		if ($strength & 1) {
			$consonants .= 'BDGHJLMNPQRSTVWXZ';
		}
		if ($strength & 2) {
			$vowels .= "AEUY";
		}
		if ($strength & 4) {
			$consonants .= '23456789';
		}
		if ($strength & 8) {
			$consonants .= '@#$%';
		}
	 
		$randomValue = '';
		$alt = time() % 2;
		for ($i = 0; $i < $length; $i++) {
			if ($alt == 1) {
				$randomValue .= $consonants[(rand() % strlen($consonants))];
				$alt = 0;
			} else {
				$randomValue .= $vowels[(rand() % strlen($vowels))];
				$alt = 1;
			}
		}

		return $randomValue;
    }

	function verfiryGenerate( $id ){
		
		$encrypt_key = $this->config->encrypt_key;
		$decrypt_key = $this->config->decrypt_key;
		
		$filter = new Zend_Filter_Encrypt ( array( 'key' => $encrypt_key ) );
		
		$encrypted = str_replace("=","_", base64_encode( $filter->filter( $id ) ) );
		
		return $encrypted;
	}
	
	public function generateProfileImages( $fileSource, $fileTarget, $fileTargetIcon, $files ){
		// CRIA IMAGEM DO PERFIL
		$image  = Third_Image::create( $fileSource );
		$image->resize(100, 100);
		$image->save($fileTarget, 'jpg');
		
        // CRIA ICONE
        $image  = Third_Image::create( $fileTarget );
		$image->adaptiveResize(50,50);
		$image->save($fileTargetIcon, 'jpg');

		// REMOVE IMAGEM TEMPORARIA
		$files->remove( $fileSource );
	}
}
?>